/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package model;

import java.util.Arrays;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.DuplicateKeyException;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databean.EmployeeBean;


public class EmployeeDAO {
	private BeanFactory<EmployeeBean> factory;
	
	public EmployeeDAO() throws DAOException {
		try {
			// Get a BeanTable so we can create the "user" table
	        BeanTable<EmployeeBean> employeeTable = BeanTable.getInstance(EmployeeBean.class,"employee");
	        
	        if (!employeeTable.exists()) employeeTable.create("userName");
	        
	        // Long running web apps need to clean up idle database connections.
	        // So we can tell each BeanTable to clean them up.  (You would only notice
	        // a problem after leaving your web app running for several hours.)
	        employeeTable.setIdleConnectionCleanup(true);
	
	        // Get a BeanFactory which the actions will use to read and write rows of the "user" table
	        factory = employeeTable.getFactory();
	        
	        
	        //create pre-defined user 1
	        if (lookup("Eric") == null)
	        {
		        //insert three users as a default when creating the user table
		        try {
	
		        	Transaction.begin();

		        	EmployeeBean dbCustomer = factory.create("Eric");
		        	dbCustomer.setPassword("Eric");
		        	dbCustomer.setFirstName("Eric");
		        	dbCustomer.setLastName("Lin");

		        	Transaction.commit();
	
				} catch (DuplicateKeyException e) {
					throw new DAOException("Username 'Eric' already exists");
				} catch (RollbackException e) {
					throw new DAOException(e);
				} finally {
					if (Transaction.isActive()) Transaction.rollback();
				}
	        }
	        
	        
	        //create pre-defined user 2
	        if (lookup("Yui") == null)
	        {
		        //insert three users as a default when creating the user table
		        try {
	
		        	Transaction.begin();
		        	factory = employeeTable.getFactory();

		        	EmployeeBean dbCustomer = factory.create("Yui");
		        	dbCustomer.setPassword("Yui");
		        	dbCustomer.setFirstName("Yui");
		        	dbCustomer.setLastName("Rasananda");
		        	
		        	Transaction.commit();
	
				} catch (DuplicateKeyException e) {
					throw new DAOException("Username 'Yui' already exists");
				} catch (RollbackException e) {
					throw new DAOException(e);
				} finally {
					if (Transaction.isActive()) Transaction.rollback();
				}
	        }
	        
	        
	        //create pre-defined user 3
	        if (lookup("Alex") == null)
	        {
		        //insert three users as a default when creating the user table
		        try {
	
		        	Transaction.begin();
		        	factory = employeeTable.getFactory();
		        	
		        	EmployeeBean dbCustomer = factory.create("Alex");
		        	dbCustomer.setPassword("Alex");
		        	dbCustomer.setFirstName("Alex");
		        	dbCustomer.setLastName("Yuan");
		        	
		        	Transaction.commit();
	
				} catch (DuplicateKeyException e) {
					throw new DAOException("Username 'Alex' already exists");
				} catch (RollbackException e) {
					throw new DAOException(e);
				} finally {
					if (Transaction.isActive()) Transaction.rollback();
				}
	        }
	        
	        
	      //create pre-defined user 4
	        if (lookup("Nurlan") == null)
	        {
		        //insert three users as a default when creating the user table
		        try {
	
		        	Transaction.begin();
		        	factory = employeeTable.getFactory();
		        	
		        	EmployeeBean dbCustomer = factory.create("Nurlan");
		        	dbCustomer.setPassword("Nurlan");
		        	dbCustomer.setFirstName("Nurlan");
		        	dbCustomer.setLastName("Karimzhan");
		        	
		        	Transaction.commit();
	
				} catch (DuplicateKeyException e) {
					throw new DAOException("Username 'Nurlan' already exists");
				} catch (RollbackException e) {
					throw new DAOException(e);
				} finally {
					if (Transaction.isActive()) Transaction.rollback();
				}
	        }
	        
	        
	      //create pre-defined user 5
	        if (lookup("Himani") == null)
	        {
		        //insert three users as a default when creating the user table
		        try {
	
		        	Transaction.begin();
		        	factory = employeeTable.getFactory();
		        	
		        	EmployeeBean dbCustomer = factory.create("Himani");
		        	dbCustomer.setPassword("Himani");
		        	dbCustomer.setFirstName("Himani");
		        	dbCustomer.setLastName("Gupta");
		        	
		        	Transaction.commit();
	
				} catch (DuplicateKeyException e) {
					throw new DAOException("Username 'Himani' already exists");
				} catch (RollbackException e) {
					throw new DAOException(e);
				} finally {
					if (Transaction.isActive()) Transaction.rollback();
				}
	        }
	        
	        
	        
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public void create(EmployeeBean employee) throws DAOException {
		try {

        	if (lookup(employee.getUserName())==null) {
        			        	
	        	Transaction.begin();
	        	
	        	EmployeeBean dbEmployee = factory.create(employee.getUserName());
				factory.copyInto(employee,dbEmployee);
				
				Transaction.commit();
	        	
        	} else {
        		
        		throw new DuplicateKeyException("A user named "+employee.getUserName()+" already exists");
        		
        	}
			
		} catch (DuplicateKeyException e) {
			throw new DAOException("A user named "+employee.getUserName()+" already exists");
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	/**
	 * lookup EmployeeBean by username
	 * @param userName
	 * @return
	 * @throws DAOException
	 */
	public EmployeeBean lookup(String userName) throws DAOException {
		try {
			return factory.lookup(userName);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	
	
	
	protected BeanFactory<EmployeeBean> getFactory() { return factory; }
	
	public EmployeeBean[] getEmployees() throws DAOException {
		try {
			EmployeeBean[] employees = factory.match();
			Arrays.sort(employees);
			return employees;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	public void setPassword(String username, String password) throws DAOException {
        try {
        	Transaction.begin();
        	EmployeeBean dbEmployee = lookup(username);
			
			if (dbEmployee == null) {
				throw new DAOException("User "+username+" no longer exists");
			}
			
			dbEmployee.setPassword(password);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
}
